//有效期时间插件设置
laydate.render({
    elem: '#expireTime'
});


//设置有效期限类型，1 为发放后计算天数 2 为设置具体的期限
$("input[name='coupon_type']").change(function () {
    var validConf = $("input[name='coupon_type']:checked").val();
    if (validConf == "1") {
        $("#li-reachAmount").hide();
    }
    if (validConf == "2") {
        $("#li-reachAmount").show();
    }
});

//设置有效期限类型，1 为发放后计算天数 2 为设置具体的期限
$("input[name='validConf']").change(function () {
    var validConf = $("input[name='validConf']:checked").val();
    if (validConf == "1") {
        $("#li-validityDays").show();
        $("#li-expireTime").hide();
        $("#expireTime").val("");
    }
    if (validConf == "2") {
        $("#li-validityDays").hide();
        $("#li-expireTime").show();
        $("#validityDays").val("");
    }
});

function submit() {

    var title = $("#title").val();
    var type = $("input[name='coupon_type']:checked").val();
    var reachAmount = parseInt($("#reachAmount").val());
    var denomination = parseInt($("#denomination").val());
    var limitMember = $("input[name='limitMember']:checked").val();
    var validityDays = $("#validityDays").val();
    var expireTime = $("#expireTime").val();
    var diffTag = $("input[name='diffTag']:checked").val();
    if(denomination>=500){
        alert("优惠券面值不得大于500");
        return;
    }
    if((type=="2"&&isNaN(reachAmount))||isNaN(denomination)){
        alert("请输入数字");
        return;
    }
    if(type=="2"&&reachAmount!=""&&(reachAmount<denomination)) {
        alert("满减的条件面值不小于减的面值");
        return;
    }
    if (!validForm()) {
        alert("请完善优惠券必填信息");
        return;
    }
    var displayName="";
    if(type=="1"){
        displayName="无门槛"+denomination+"元";
    }else{
        displayName="满"+reachAmount+"减"+denomination;
    }
    if(expireTime==""){
        expireTime=null;
    }
    console.log("title:", title);
    console.log("type:", type);
    console.log("reachAmount:", reachAmount);
    console.log("denomination:", denomination);
    console.log("limitMember:", limitMember);
    console.log("validityDays:", validityDays);
    console.log("expireTime:", expireTime);
    console.log("diffTag:", diffTag);

    var coupon={
        title:title,
        displayName:displayName,
        type:type,
        reachAmount:reachAmount,
        denomination:denomination,
        limitMember:limitMember,
        validityDays:validityDays,
        expireTime:expireTime,
        diffTag:diffTag
    };
    $.ajax({
        type: "POST",
        url: urlcore + "/api/coupon/add",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(coupon),
        success: function (data) {
            alert("优惠券添加成功！");
            window.location.href = "coupon_list.html";
        },
        error: function () {
            alert("error")
        }
    });

    //校验表单
    function validForm() {
        var result = true;
        if (title == "" || denomination == "") {
            return false;
        }
        var validConf = $("input[name='coupon_type']:checked").val();
        if (validConf == "2") {
            if (reachAmount == "") {
                return false
            }
        }else{

        }
        var validConf = $("input[name='validConf']:checked").val();
        if (validConf == "1") {
            if (validityDays == "") {
                return false
            }
        }
        if (validConf == "2") {
            if (expireTime == "") {
                return false
            }
        }
        return result;
    }

}

//number only
$('input[class*="numbersOnly"]').keyup(function () {
    if (this.value != this.value.replace(/[^\0-9\.]/g, "")) {
        this.value = this.value.replace(/[^\0-9\.]/g, "");
    }
});

